package com.junchao.peng.service.impl;

import com.junchao.peng.mapper.FileMapper;
import com.junchao.peng.pojo.File;
import com.junchao.peng.service.FileService;
import com.junchao.peng.utils.JsonResult;
import com.junchao.peng.utils.UploadFileService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

/**
 * @author dell
 */
@Service("FileService")
public class FileServiceImpl implements FileService {

    final FileMapper fileMapper;
    final RedisTemplate redisTemplate;
    final UploadFileService uploadFile;

    public FileServiceImpl(FileMapper fileMapper, RedisTemplate redisTemplate, UploadFileService uploadFile) {
        this.fileMapper = fileMapper;
        this.redisTemplate = redisTemplate;
        this.uploadFile = uploadFile;
    }

    @Override
    public JsonResult deleteByPrimaryKey(Integer id, String token) {
        File file = fileMapper.selectByPrimaryKey(id);
        String s = uploadFile.deleteFile(file.getPath());
        if ("没有原文件路径，不做删除处理".equals(s)){
            return new JsonResult("文件不存在，删除失败",404);
        }
        int i = fileMapper.deleteByPrimaryKey(id);
        if (i != 0){
            return new JsonResult(s);
        }
        return new JsonResult("删除失败请联系管理员");
    }

    @Override
    public JsonResult insert(File file, String token) {
        return null;
    }

    @Override
    public JsonResult insertSelective(MultipartFile file, String token) {
        //拿源文件的文件名
        String originalFilename = file.getOriginalFilename();
        //拿用户上传文件的后缀名
        String originalFilename2= Objects.requireNonNull(originalFilename).substring(originalFilename.lastIndexOf("."));

        File file1 = new File(null, originalFilename, (double) file.getSize(), originalFilename2, null);
        try {
            String fileUrl = uploadFile.uploadFile(file,"");
            if (fileUrl != null){
                file1.setPath(fileUrl);
                int i = fileMapper.insertSelective(file1);
                if (i != 0){
                    return new JsonResult("文件上传成功 路径："+fileUrl);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new JsonResult("出现错误，请联系管理员",500);
    }

    @Override
    public JsonResult selectByPrimaryKey(Integer id, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        return new JsonResult(fileMapper.selectByPrimaryKey(id));
    }

    @Override
    public JsonResult selectByUrl(String url, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        return new JsonResult(fileMapper.selectByUrl(url));
    }

    @Override
    public JsonResult listFile(String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        return new JsonResult(fileMapper.listFile());
    }

    private boolean getToken(String token){
        boolean flag = false;
        Object redisCode = redisTemplate.opsForValue().get(token);
        if (redisCode != null){
            flag = true;
        }
        return !flag;
    }
}
